home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / memory / xms200je / umbtest.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-22  |  7.0 KB  |  261 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //      UMBTEST.CPP: Test program for UMB class.
  4. //      Copyright (c) J.English 1993.
  5. //      Author's address: je@unix.brighton.ac.uk
  6. //
  7. //      Permission is granted to use copy and distribute the
  8. //      information contained in this file provided that this
  9. //      copyright notice is retained intact and that any software
  10. //      or other document incorporating this file or parts thereof
  11. //      makes the source code for the library of which this file
  12. //      is a part freely available.
  13. //
  14. //--------------------------------------------------------------------------
  15. //
  16. //      Revision history:
  17. //      2.0     Nov 1993        Initial coding
  18. //
  19. //--------------------------------------------------------------------------
  20.  
  21. #include "xms.h"
  22. #include "doserror.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <time.h>
  26.  
  27.  
  28. //--------------------------------------------------------------------------
  29. //
  30. //      Global data.
  31. //
  32. DOSerror e;             // guard against critical errors and control-breaks
  33. UMB* desc [10];         // array of pointers to UMB descriptors
  34. int ndesc = 0;          // number of descriptors
  35.  
  36.  
  37. //--------------------------------------------------------------------------
  38. //
  39. //      Function prototypes.
  40. //
  41. char menu ();           // display menu & get user's choice
  42. void UMBstats ();       // 1. UMB statistics
  43. void blocksizes ();     // 2. List block sizes
  44. void allocate ();       // 3. Allocate block
  45. void deallocate ();     // 4. Deallocate block
  46. void copy ();           // 5. Copy block
  47.  
  48. //--------------------------------------------------------------------------
  49. //
  50. //      Main program.
  51. //
  52. //      A menu of choices is displayed, allowing the user to exercise
  53. //      each of the UMB functions available.
  54. //
  55. void main ()
  56. {
  57.     char choice;
  58.     UMBstats ();
  59.     for (;;)
  60.     {
  61.         choice = menu ();
  62.         if (choice == 'X')
  63.             break;
  64.  
  65.         switch (choice)
  66.         {
  67.             case '1':
  68.                 UMBstats ();
  69.                 break;
  70.             case '2':
  71.                 blocksizes ();
  72.                 break;
  73.             case '3':
  74.                 allocate ();
  75.                 break;
  76.             case '4':
  77.                 deallocate ();
  78.                 break;
  79.             case '5':
  80.                 copy ();
  81.                 break;
  82.         }
  83.     }
  84.     for (int i = 0; i < ndesc; i++)
  85.         delete desc[i];
  86. }
  87.  
  88. //--------------------------------------------------------------------------
  89. //
  90. //      Display menu and get user choice.
  91. //
  92. //      The choice must be a line containing a single character from 1 to 4
  93. //      (or X for exit).
  94. //
  95. char menu ()
  96. {
  97.     printf ("\nChoose desired operation:\n"
  98.             "  1) Display UMB statistics\n"
  99.             "  2) List block sizes\n"
  100.             "  3) Allocate block\n"
  101.             "  4) Deallocate block\n"
  102.             "  5) Copy block\n"
  103.             "  X) Exit program\n");
  104.     char buff [80];
  105.     for (;;)
  106.     {
  107.         printf ("Enter your choice: ");
  108.         fgets (buff, 80, stdin);
  109.         if ((buff[0] == 'x' || buff[0] == 'X') && buff[1] == '\n')
  110.             return 'X';
  111.         if (buff[0] < '1' || buff[0] > '5' || buff[1] != '\n')
  112.             printf ("Invalid choice!\a\n");
  113.         else
  114.             break;
  115.     }
  116.     printf ("\n");
  117.     return buff[0];
  118. }
  119.  
  120.  
  121. //--------------------------------------------------------------------------
  122. //
  123. //      Display UMB statistics.
  124. //
  125. void UMBstats ()
  126. {
  127.     printf ("Largest available UMB block: %ld bytes.\n", UMB::largest());
  128. }
  129.  
  130.  
  131. //--------------------------------------------------------------------------
  132. //
  133. //      List block sizes.
  134. //
  135. void blocksizes ()
  136. {
  137.     int flag = 0;
  138.     for (int i = 0; i < ndesc; i++)
  139.     {   if (desc[i] != 0)
  140.         {   printf ("Block %d: size = %ld\n", i+1, desc[i]->size());
  141.             flag = 1;
  142.         }
  143.     }
  144.     if (flag == 0)
  145.         printf ("No blocks allocated!\n");
  146. }
  147.  
  148. //--------------------------------------------------------------------------
  149. //
  150. //      Allocate block.
  151. //
  152. //      Attempt to allocate a new block of a specified size and report the
  153. //      result.
  154. //
  155. void allocate ()
  156. {
  157.     printf ("Enter block size: ");
  158.     long size;
  159.     scanf ("%ld", &size);
  160.     while (getchar() != '\n')
  161.         continue;
  162.     desc [ndesc++] = new UMB (size);
  163.     printf ("Block %d: ", ndesc);
  164.     printf ("requested %ld bytes, granted %ld bytes (",
  165.             size, desc[ndesc-1]->size());
  166.     if (!desc[ndesc-1]->valid())
  167.     {   printf ("error code %02X", !*desc[ndesc-1]);
  168.         delete desc[--ndesc];
  169.     }
  170.     else
  171.         printf ("address %p", desc[ndesc-1]->addr());
  172.     printf (").\n");
  173. }
  174.  
  175.  
  176. //--------------------------------------------------------------------------
  177. //
  178. //      Deallocate block.
  179. //
  180. //      Attempt to deallocate an existing block and report the result.
  181. //
  182. void deallocate ()
  183. {
  184.     printf ("Enter block number: ");
  185.     int b;
  186.     scanf ("%d", &b); b--;
  187.     while (getchar() != '\n')
  188.         continue;
  189.     if (b < 0 || b >= ndesc || desc[b] == 0)
  190.         printf ("No such block!\n");
  191.     else
  192.     {   delete desc[b];
  193.         desc[b] = 0;
  194.         printf ("Block %d deallocated.\n", b+1);
  195.     }
  196. }
  197.  
  198. //--------------------------------------------------------------------------
  199. //
  200. //      Copy to/from UMB block.
  201. //
  202. //      Prompts for a block number.  An array of random numbers of the
  203. //      specified size is created in conventional memory, copied to the
  204. //      specified block starting at the specified offset, copied back to
  205. //      conventional memory, and finally compared with the original block.
  206. //
  207. void copy ()
  208. {
  209.     unsigned i;
  210.     printf ("Enter block number: ");
  211.     int b;
  212.     scanf ("%d", &b); b--;
  213.     while (getchar() != '\n')
  214.         { ; }
  215.     if (b < 0 || b >= ndesc || desc[b] == 0)
  216.     {   printf ("No such block!\n");
  217.         return;
  218.     }
  219.     char far* umb = (char far*) desc[b]->addr();
  220.     unsigned s = desc[b]->size();
  221.     if (s != desc[b]->size())
  222.     {   printf ("Block larger than 64K; "
  223.                 "only the first 64K-1 bytes will be copied\n");
  224.         s = 0xFFFF;
  225.     }
  226.     char* x = new char [s];
  227.     char* y = new char [s];
  228.     if (x == 0 || y == 0)
  229.     {   printf ("Not enough real memory!\n");
  230.         return;
  231.     }
  232.  
  233.     printf ("Generating random data... "); fflush (stdout);
  234.     randomize ();
  235.     for (i = 0; i < s; i++)
  236.         x[i] = random (256);
  237.     printf ("done.\n");
  238.  
  239.     printf ("Copying random data to UMB... "); fflush (stdout);
  240.     for (i = 0; i < s; i++)
  241.         umb[i] = x[i];
  242.     printf ("done.\n");
  243.  
  244.     printf ("Copying random data back from UMB... "); fflush (stdout);
  245.     for (i = 0; i < s; i++)
  246.         y[i] = umb[i];
  247.     printf ("done.\n");
  248.  
  249.     for (i = 0; i < s; i++)
  250.     {   if (x[i] != y[i])
  251.         {   printf ("Verification error at offset %u.\n", i);
  252.             break;
  253.         }
  254.     }
  255.     if (i == s)
  256.         printf ("Copy verified successfully.\n");
  257.  
  258.     delete x;
  259.     delete y;
  260. }
  261.